package com.example.shop_2208.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2208.R
import com.example.shop_2208.databinding.ActivityLoginBinding
import com.example.shop_2208.db.MyDbUtils
import com.example.shop_2208.mvvm.BaseActivity
import com.example.shop_2208.net.Const
import com.example.shop_2208.register.RegisterActivity
import kotlinx.coroutines.launch

/**
 * @Route 路由注解 ：定义跳转页面的路径名称  名称规范/XXX/XXX  同一个App下前方名称一致 不同APP的前方名称不能相同
 */
@Route(path = "/app/login")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun initData() {
        viewModel.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==Const.CODE_SUCCESS){
                SPUtils.getInstance().put(Const.SP_TOKEN,it.data.token)
                SPUtils.getInstance().put("id",it.data.id)//保存用户id 方便后续在我的界面里进行查询显示
                //在登录成功时，将用户地址保存---方便在订单界面展示和修改用户地址
                SPUtils.getInstance().put("address",it.data.address)
                //MyDbUtils.getDb().getUserDao().insertUser(it.data)
                lifecycleScope.launch {
                    MyDbUtils.getDb().getUserDao().insertUser(it.data)
                }
                ARouter.getInstance().build("/app/main").withSerializable("user",it.data).navigation()
            }
        }
    }

    override fun initView() {
        binding.login.setOnClickListener {
            val username = binding.etUsername.text.toString()
            val password = binding.etPassword.text.toString()
            if(username.isEmpty()||password.isEmpty()){
                ToastUtils.showLong("用户名、密码不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(username)){
                ToastUtils.showLong("用户名的格式不正确，应该为手机号")
            }else if(!RegexUtils.isMatch("^[\\w]{6,8}\$",password)){
                ToastUtils.showLong("密码格式不正确，应该为6-8位的数字、字母、下划线组合")
            }else{
                viewModel.login(binding.etUsername.text.toString(),binding.etPassword.text.toString())
            }

        }
        binding.goReg.setOnClickListener {
           /* val intent = Intent(this,RegisterActivity::class.java)
            startActivityForResult(intent,101)*/
            ARouter.getInstance().build("/app/register").navigation(this,101)
        }

    }

    override fun getViewModel() = LoginViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_login
    //接收注册成功后的返回值
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==101&&resultCode==102){
            val username = data?.extras?.getString("username")
            val password = data?.extras?.getString("password")
            binding.etUsername.setText(username)
            binding.etPassword.setText(password)
        }
    }

}